using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.APPLications.Commands.TraceabilityInfoshow;
using Traceability.API.Services;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.APPLications.CommandHanders.TraceabilityInfoshow
{
    /// <summary>
    /// 恢复已删除溯源信息命令处理程序（注意：已改为物理删除，此功能不再适用）
    /// </summary>
    public class RestoreTraceabilityInfoCommandHandler : IRequestHandler<RestoreTraceabilityInfoCommand, APIResult<bool>>
    {
        private readonly IBaseRepository<TraceabilityInfo> _traceabilityRepository;
        private readonly TraceabilityRedisHelper _redisHelper;
        private readonly ILogger<RestoreTraceabilityInfoCommandHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="traceabilityRepository">溯源信息仓储</param>
        /// <param name="redisHelper">Redis帮助类</param>
        /// <param name="logger">日志记录器</param>
        public RestoreTraceabilityInfoCommandHandler(
            IBaseRepository<TraceabilityInfo> traceabilityRepository,
            TraceabilityRedisHelper redisHelper,
            ILogger<RestoreTraceabilityInfoCommandHandler> logger = null)
        {
            _traceabilityRepository = traceabilityRepository;
            _redisHelper = redisHelper;
            _logger = logger;
        }

        /// <summary>
        /// 处理恢复请求
        /// </summary>
        /// <param name="request">恢复请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>恢复结果</returns>
        public async Task<APIResult<bool>> Handle(RestoreTraceabilityInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<bool> result = new APIResult<bool>();

            try
            {
                _logger?.LogInformation("收到恢复请求: ID={0}, ModifiedBy={1} - 但系统已改为物理删除，无法恢复", request.TraceabilityId, request.ModifiedBy);

                // 由于已改为物理删除，无法恢复已删除的记录
                result.Code = APIEnum.操作失败;
                result.Message = "系统已改为物理删除模式，删除的记录无法恢复。如需恢复数据，请联系系统管理员从备份中恢复。";
                result.Data = false;

            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "恢复处理时发生异常: ID={0}", request.TraceabilityId);
                result.Code = APIEnum.操作失败;
                result.Message = $"恢复操作失败: {ex.Message}";
                result.Data = false;
            }

            return result;
        }
    }
} 